package com.ceshiren.hogwarts.restassured.wechat.apiobject.framwork.steps;

import com.apiobject.utils.PlaceholderUtils;
import com.ceshiren.hogwarts.restassured.wechat.apiobject.framwork.global.ApiLoader;
import com.ceshiren.hogwarts.restassured.wechat.apiobject.framwork.global.GlobalVariables;
import com.ceshiren.hogwarts.restassured.wechat.apiobject.framwork.test.Test02_ApiObjectModelTest;
import io.restassured.response.Response;
import org.junit.jupiter.api.function.Executable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;


/**
 * @author Seven
 * @date 2020/12/30 16:47
 */
public class StepModel {
    /**
     * 1.需要定义AssertModel类
     */
    private String api;
    private String action;
    private ArrayList<String> actualParameter;
    private ArrayList<AssertModel> asserts;
    private HashMap<String,String> save;
    private HashMap<String,String> saveGlobal;
    /**
     * 2.需要定义StepResult类
     */
    private ArrayList<String> finalActualParameter = new ArrayList<>();
    private HashMap<String,String> stepVariables = new HashMap<>();
    private StepResult stepResult = new StepResult();
    private ArrayList<Executable> assertList = new ArrayList<>();

    public static final Logger logger = LoggerFactory.getLogger(Test02_ApiObjectModelTest.class);


    public String getApi() {
        return api;
    }

    public void setApi(String api) {
        this.api = api;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public ArrayList<String> getActualParameter() {
        return actualParameter;
    }

    public void setActualParameter(ArrayList<String> actualParameter) {
        this.actualParameter = actualParameter;
    }

    public ArrayList<AssertModel> getAsserts() {
        return asserts;
    }

    public void setAsserts(ArrayList<AssertModel> asserts) {
        this.asserts = asserts;
    }

    public HashMap<String, String> getSave() {
        return save;
    }

    public void setSave(HashMap<String, String> save) {
        this.save = save;
    }

    public HashMap<String, String> getSaveGlobal() {
        return saveGlobal;
    }

    public void setSaveGlobal(HashMap<String, String> saveGlobal) {
        this.saveGlobal = saveGlobal;
    }

    /**
     * @param testCaseVariables 每一个返回的step内的变量都会存储在这里
     * @return
     */
    public StepResult run(HashMap<String,String> testCaseVariables){
        /**
         * 3.需要定义AssertModel类
         */
        if(actualParameter!=null){
            finalActualParameter.addAll(PlaceholderUtils.resolveList(actualParameter,testCaseVariables));
        }
        /**
         * 4.根据case中配置Api对象和action信息，取出并执行相应action
         */
        Response response = ApiLoader.getAction(api,action).run(finalActualParameter);
        /**
         * 5.处理yaml里的save
         */
        if(save !=null){
            save.forEach((variablesName,path) ->{
                String value = response.path(path).toString();
                stepVariables.put(variablesName,value);
                logger.info("step变量更新:"+ stepVariables);
            });
        }

        /**
         * 6.存储global（emg：token）
         */
        if (saveGlobal!=null){
            saveGlobal.forEach((variablesName,path)->{
                String value = response.path(path);
                GlobalVariables.getGlbalVariables().put(variablesName,value);
                logger.info("全局变量更新："+GlobalVariables.getGlbalVariables());
            });
        }

        /**
         * 7.处理软断言需要的中间断言数据
         */
        if(asserts !=null){
            asserts.stream().forEach(assertModel ->{
                assertList.add(()->{
                    assertThat(assertModel.getReason(),response.path(assertModel.getActual()).toString(),equalTo(assertModel.getExpect()));
                });
                    }
            );
        }

        /**
         * 8.将respose和断言结果存储到stepResult对象中并返回
         */
        stepResult.setArrayList(assertList);
        stepResult.setStepVariables(stepVariables);
        stepResult.setResponse(response);
        return stepResult;

    }

}
